package com.xiaoshu.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import com.xiaoshu.model.Video;
import com.xiaoshu.service.VideoService;
import com.xiaoshu.util.VideoUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.Ehcache;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoshu
 * @description
 * @date 2023年11月11日 1:32
 */
@Slf4j
@Service
public class VideoServiceImpl implements VideoService {

    @Value("${video.init-url}")
    private String initUrl;

    @Resource
    private CacheManager cacheManager;

    @Override
    public List<Video> allVideos() {
        Cache cache = cacheManager.getCache("all-videos");
        return cache.get("all-videos",List.class);
    }

    @PostConstruct
    public void init(){
        log.info("初始化视频仓库...");
        Cache cache = cacheManager.getCache("all-videos");
        List<Video> list = cache.get("all-videos", List.class);
        if (CollectionUtil.isNotEmpty(list)){
            return;
        }
        File file = new File(initUrl);
        File[] files = file.listFiles();
        if (ArrayUtil.isEmpty(files)){
            log.info("目录内容为空");
        }
        List<Video> videos = new LinkedList<>();
        getVideo(videos,files);
        log.info("读取视频条数："+videos.size());
        cache.put("all-videos",videos);
    }

    private static final List<String> VIDEO_TYPE = Arrays.asList("avi", "wmv", "mpeg","m4v","mov","asf","flv","f4v","rmvb","mkv");

    public void getVideo(List<Video> videos,File[] files){

        Arrays.stream(files).forEach(e->{
            if (e.isFile()){
                String prefix = FileUtil.getSuffix(e);
                if (VIDEO_TYPE.contains(prefix)){
                    Video video = new Video();
                    video.setUrl(VideoUtil.getVideoUrl(initUrl,e.getAbsolutePath()));
                    video.setType("video/"+FileUtil.getSuffix(e));
                    videos.add(video);
                }
            }else {
                getVideo(videos,e.listFiles());
            }
        });
    }

    public static void reNameFiles(File[] files){
        log.info("处理数量："+files.length);
        if (ArrayUtil.isNotEmpty(files)){
            Arrays.stream(files).forEach(e->{
                if (e.isFile()){
                    String suffix = FileUtil.getSuffix(e);
                    FileUtil.rename(e, IdUtil.getSnowflakeNextId()+"."+suffix,true);
                }else {
                    reNameFiles(e.listFiles());
                    FileUtil.rename(e,IdUtil.getSnowflakeNextId()+"",false);
                }
            });
        }
        log.info("处理结束");
    }

    public static void main(String[] args) {
        File file = new File("F:\\ToonE\\里番");
        reNameFiles(file.listFiles());
    }

}
